/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {
    var index = function() {
        this.init();
        this.total=0;
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'site_service_kefus',
                model: {
                    url:_global.url.api + 'site_service_kefu',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove'
                    },
                    edit: function(e) {
                        _this.data = this.data;

                        e.preventDefault();
                        _this.dialog = dialog({
                            title:'修改客服',
                            content: template('add-tpl', this.data),
                            width: 900,
                            ok: function() {
                                _this.data.mobile = $.trim($('#mobile').val());
                                if( _this.data.mobile==''){
                                    Alert({msg:'请输入手机号码',type:'error'});
                                    return false;
                                }

                                _this.data.nickname = $('#nickname').val();

                                if(_this.data.nickname==''){
                                    Alert({msg:'用户昵称不能为空',type:'error'});
                                    return false;
                                }

                                _this.data.password = $.trim($('#password').val());
                                r_password = $.trim($('#r_password').val());

                                if(_this.data.password!=r_password){
                                    Alert({msg:'两次密码不一致',type:'error'});
                                    return false;
                                }

                                _this.data.name = $.trim($('#name').val());

                                if(new Model({
                                        url: _global.url.api + 'site_service_kefu',
                                        data: _this.data
                                    }).put()) {
                                    _this.collection.reload();
                                }
                            },cancel: function() {

                            }
                        });
                        _this.dialog.show();
                        $('input,select,textarea').on('change', function(e) {
                            _this.change(e);
                        });
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.reload();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    if(this.result.data.length==0){
                        $('#top_r').html('您还没开通默认客服，请联系管理员！');
                    }
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function(e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title:'添加客服',
                    content: template('add-tpl', {}),
                    width: 900,
                    ok: function() {
                        _this.data.mobile = $.trim($('#mobile').val());
                        if( _this.data.mobile==''){
                            Alert({msg:'请输入手机号码',type:'error'});
                            return false;
                        }

                        _this.data.nickname = $('#nickname').val();

                        if(_this.data.nickname==''){
                            Alert({msg:'用户昵称不能为空',type:'error'});
                            return false;
                        }

                        _this.data.password = $.trim($('#password').val());
                        r_password = $.trim($('#r_password').val());

                        if(_this.data.password==''){
                            Alert({msg:'密码不能为空',type:'error'});
                            return false;
                        }
                        if(_this.data.password!=r_password){
                            Alert({msg:'两次密码不一致',type:'error'});
                            return false;
                        }

                        _this.data.name = $.trim($('#name').val());

                        if(new Model({
                                url: _global.url.api + 'site_service_kefu',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    },cancel: function() {

                    }
                });
                _this.dialog.show();
                $('input,select,textarea').on('change', function(e) {
                    _this.change(e);
                });
            });
        },
        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();

});